package com.hrbxlh.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.NoticeActionImpl;
import com.hrbxlh.util.PropertiesUtil;


/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：ClassAction.java
* Description：消息action类
* History：
* Date           Author               Desc
* 2017.3.29		  sy                   建立
* -------------------------------------------------------
*/
@Controller
public class NoticeAction {
	
	@Autowired
	@Qualifier("noticeActionImpl")
	private NoticeActionImpl noticeActionImpl;
	
	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;
	
	/**
	 * 
	 * @return 查询通知设置。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getNotice", method = RequestMethod.GET)
	public ModelAndView getNotice(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MemberAction.class.getName());
		try {
			
			
			//获取消息信息
			Map<String, Object> resultAllMap = noticeActionImpl.getNotice(session);
			//查询当前人的消息
			List<Map<String, Object>> noticeList = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//返回值
			resultMap.put("noticeMap", resultAllMap.get("noticeMap"));
			resultMap.put("classList", resultAllMap.get("classList"));
			resultMap.put("memberList", resultAllMap.get("memberList"));
		    //人员列表页面名称
			pageName = "pc/noticeSet";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("消息设置异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  跳转到添加消息设置页面
	 * @throws IOException
	 */
	@RequestMapping(value = "/AddNoticeView", method = RequestMethod.GET)
	public ModelAndView AddNoticeView(String jsonStr,HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		System.out.println("添加消息设置页面");
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		
		try {
			
			Map<String, Object> resultAllMap = noticeActionImpl.getNotice(session);
			//返回值
			resultMap.put("noticeMap", resultAllMap.get("noticeMap"));
			resultMap.put("classList", resultAllMap.get("classList"));
			resultMap.put("memberList", resultAllMap.get("memberList"));
			resultMap.put("noticeClassList", resultAllMap.get("noticeClassList"));
			String[] interfaceNameArrUse = {"server_address","project_name"};
			String[] valueArrUse = PropertiesUtil.getUrl(interfaceNameArrUse);
			String urlUse = "";
			for(String value:valueArrUse){
				urlUse += value;
			}
			//返回图片地址
            String returnUrl= urlUse+"images/pc";
            resultMap.put("imageUrl", returnUrl);
           //查询当前人的消息
			List<Map<String, Object>> noticeList = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//添加页面名称
			pageName = "pc/addNotice";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转消息页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}

	/**
	 * @param request
	 * @param response
	 * @return  消息设置。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addNoticeSet", method = RequestMethod.POST)
	public void addNoticeSet(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		String edit="";
		try {
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			edit = (String) paramMap.get("noticeContent");
			if (paramMap.get("noticeContent")== null||paramMap.get("noticeContent").equals("")) {
				edit = "";
			} else if (edit.length() == 0) {
				edit = "";
			}
			edit = edit.replaceAll("\n", "<br>");
			edit = edit.replaceAll("\r", "<br>");
			paramMap.put("noticeContent", edit);
			//消息设置。
			boolean resultFlag = noticeActionImpl.addNoticeSet(paramMap,session);
			resultMark = (resultFlag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("errorMsg", "设置失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加消息出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  跳转到帮助文档页面
	 * @throws IOException
	 */
	@RequestMapping(value = "/adminHelpAction", method = RequestMethod.GET)
	public ModelAndView adminHelpAction(HttpServletRequest request, HttpSession session) throws IOException {		
		System.out.println("帮助文档页面");
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		
		try {
			//查询当前人的消息
			List<Map<String, Object>> noticeList = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//判断当前登陆人的身份
			int roleId = (Integer) session.getAttribute("roleId");
			if(roleId==3){
				//管理员帮助文档页面
				pageName = "pc/adminHelp";
			}if(roleId==2){
				//教师帮助文档页面
				pageName = "pc/teaHelp";
			}
			
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转消息页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}

}
